// lib/controllers/group_list_controller.dart
import 'package:chat/model/group_model.dart';
import 'package:get/get.dart';

class GroupListController extends GetxController {
  // 使用RxList存储Group对象
  final RxList<Group> groups = <Group>[].obs;

  @override
  void onInit() {
    super.onInit();
    loadGroups();
  }

  // 加载群组数据
  void loadGroups() {
    groups.assignAll([
      Group(
        id: '1',
        name: '群聊123',
        avatarUrl: 'assets/images/zh-CN-XiaochenNeural.png',
        createdAt: DateTime.now().subtract(const Duration(days: 2)),
      ),
      Group(
        id: '2',
        name: '群聊234',
        avatarUrl: 'assets/images/zh-CN-XiaoyouNeural.png',
        createdAt: DateTime.now().subtract(const Duration(days: 1)),
      ),
    ]);
  }

  // 搜索群组
  void searchGroups(String keyword) {
    if (keyword.isEmpty) {
      loadGroups();
    } else {
      groups.assignAll(
        [
          Group(
            id: '1',
            name: '群聊123',
            avatarUrl: 'assets/images/zh-CN-XiaochenNeural.png',
            createdAt: DateTime.now().subtract(const Duration(days: 2)),
          ),
          Group(
            id: '2',
            name: '群聊234',
            avatarUrl: 'assets/images/zh-CN-XiaoyouNeural.png',
            createdAt: DateTime.now().subtract(const Duration(days: 1)),
          ),
        ].where((group) => group.name.contains(keyword)).toList(),
      );
    }
  }
}
